package com.jinxiangyang.controller;

import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.Subject;
import com.jinxiangyang.domain.vo.SubjectEeVo;
import com.jinxiangyang.service.SubjectService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-08-10
 */
@RestController
@RequestMapping("/subject")
@Api(tags = "课程分类接口")
@CrossOrigin
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 课程分类 懒加载
     * @param id
     * @return
     */
    @ApiOperation("课程列表")
    @GetMapping("/{id}")
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    public AjaxResult getChildSubject(@PathVariable Long id){
        List<Subject> list = subjectService.selectSubject(id);
        return AjaxResult.success(list);
    }

    @ApiOperation("课程模糊查询")
    @GetMapping
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    private AjaxResult getSubjectLike(SubjectEeVo subjectEeVo){
        List<Subject> subjects = subjectService.selectSubjectLike(subjectEeVo);
        return AjaxResult.success(subjects);
    }

    /**
     * 课程分类列表添加
     * @param subject
     * @return
     */

    @ApiOperation("课程列表添加")
    @PostMapping
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    public AjaxResult saveSubject(@RequestBody Subject subject){
        boolean b = subjectService.save(subject);
        if (b){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 课程分类列表修改
     * @param subject
     * @return
     */

    @ApiOperation("课程分类列表修改")
    @PutMapping
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.UPDATE)
    public AjaxResult updateSubject(@RequestBody Subject subject){
        boolean b = subjectService.updateById(subject);
        if (b){
            return AjaxResult.success("修改成功",subject);
        }
        return AjaxResult.error("修改失败",subject);
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */

    @ApiOperation("课程列表删除")
    @DeleteMapping("/{id}")
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.DELETE)
    public AjaxResult deleteSubject(@PathVariable Long id){
        boolean b = subjectService.removeById(id);
        if (b){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 导出
     * @param response
     */
    @ApiOperation("课程分类导出")
    @GetMapping("/exportData")
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    public void exportData(HttpServletResponse response){
        subjectService.exportData(response);
    }

    /**
     * 导入
     * @param file
     * @return
     */

    @ApiOperation("课程分类导入")
    @PostMapping("/importData")
    @OperateLog(title = "课程分类模块",operatorType = OperatorType.TEACHER, businessType = BusinessType.OTHER)
    public AjaxResult importData(MultipartFile file){
        subjectService.importData(file);
        return AjaxResult.success(null);
    }
}

